//
//  PpBaseLancloudTool.swift
//  Palmpilot
//
//  Created by 杨伟毅 on 2018/10/23.
//  Copyright © 2018年 杨伟毅. All rights reserved.
//

import UIKit
import Foundation
import QorumLogs
typealias  completionHandler = ((Data?, URLResponse?, Error?) -> ())

class PpBaseLancloudTool{
    
    static let shared = PpBaseLancloudTool()
//    private
    private let session = URLSession.shared
    
    
    class func isTimeZone() -> Bool{
        let zone = TimeZone.current.identifier
    QL1("打印SQ\(zone)")
        let zones = ["Asia/Chongqing","Asia/Harbin","Asia/Hong_Kong","Asia/Macau","Asia/Shanghai","Asia/Taipei"]
        var isContainer = false
        zones.forEach{ if $0.contains(zone){isContainer = true}}
        return isContainer
    }
//    lazy var req:URLRequest = {
//        let urlStr = "https://\(Cloud.prefix).api.lncld.net/1.1/feedback"
//        let url = URL(string: urlStr)
//        var req = URLRequest(url: url!)
//        req.allHTTPHeaderFields = ["Content-Type":"application/json","X-LC-Key":Cloud.key,"X-LC-Id":Cloud.id]
//        req.timeoutInterval = 10
//        return req
//    }()
}
extension PpBaseLancloudTool{
    fileprivate  func postLeanData(completionHandler:@escaping completionHandler,content:String,emailStr:String){
        let urlStr = "https://\(Cloud.prefix).api.lncld.net/1.1/feedback"
        let url = URL(string: urlStr)
        var req = URLRequest(url: url!)
        req.allHTTPHeaderFields = ["Content-Type":"application/json","X-LC-Key":Cloud.key,"X-LC-Id":Cloud.id]
        req.timeoutInterval = 10
        req.httpMethod = "POST"
        // 此处传参数
        var dict = [String:Any]()
        dict[Keys.contentStr] = content
        dict[Keys.emailStr] = emailStr
        let adata = try? JSONSerialization.data(withJSONObject: dict, options: [])
        req.httpBody = adata
        
        let task = session.dataTask(with: req) { (data, response, error) in
            DispatchQueue.main.async {
                completionHandler(data,response,error)
            }
        }
        task.resume()
    }
    
    class func postDataLean(content:String,emailStr:String,completionHandler:@escaping completionHandler){
        shared.postLeanData(completionHandler: completionHandler, content: content, emailStr: emailStr)
    }
    
    fileprivate func getLeanData(completionHandler:@escaping completionHandler){
        let urlStr = "https://\(Cloud.prefix).api.lncld.net/1.1/classes/\(Cloud.tabName)"
        let url = URL(string: urlStr)
        var req = URLRequest(url: url!)
        req.allHTTPHeaderFields = ["Content-Type":"application/json","X-LC-Key":Cloud.key,"X-LC-Id":Cloud.id]
        req.timeoutInterval = 10
        req.httpMethod = "GET"
        let task = session.dataTask(with: req) { (data, response, error) in
            DispatchQueue.main.async {
                completionHandler(data,response,error)
            }
        }
        task.resume()
    }
    class func  getleanData(completionhandler:@escaping completionHandler){
        shared.getLeanData(completionHandler: completionhandler)
    }
    
}
